package com.zznode.client.wlanpolling.utils;

import java.util.HashSet;
import java.util.Set;

public class NetworkUtils {
	public final static int[] channels = new int[] {
		2412, //1
		2417, //2
		2422, //3
		2427, //4
		2432, //5
		2437, //6
		2442, //7
		2447, //8
		2452, //9
		2457, //10
		2462, //11
		2467, //12
		2472, //13
		2484,  //14
	};

	/**
	 * 整数换算成IP地址
	 * @param i
	 * @return
	 */
	public static String intToIp(int i) {
		return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF)
				+ "." + (i >> 24 & 0xFF);
	}
	
	/**
	 * 获取邻频干扰的信道
	 * @param frequency
	 * @return
	 */
	public static Set<Integer> getNeighbourDisturbs(int frequency) {
		Set<Integer> result = new HashSet<Integer>();
		
		if(frequency > 10000)
			frequency = frequency/10000;
		
		int minFrequency = frequency - 11;
		int maxFrequency = frequency + 11;
		
		for(int i = 0; i < channels.length; i++) {
			int minOtherFrequency = channels[i] - 11;
			int maxOtherFrequency = channels[i] + 11;
			
			if(minOtherFrequency <= maxFrequency && maxOtherFrequency >= maxFrequency ||
					minOtherFrequency <= minFrequency && maxOtherFrequency >= minFrequency) {
				result.add(i+1);
			}
		}
		
		return result;
	}
	
	/**
	 * 根据频率换算成通道
	 * @param frequency
	 * @return
	 */
	public static int getChannel(int frequency) {
		
		int channel = 0;
		if(frequency > 10000)
			frequency = frequency/10000;
		
		switch (frequency) {
		case 2412:
			channel = 1;
			break;
		case 2417:
			channel = 2;
			break;
		case 2422:
			channel = 3;
			break;
		case 2427:
			channel = 4;
			break;
		case 2432:
			channel = 5;
			break;
		case 2437:
			channel = 6;
			break;
		case 2442:
			channel = 7;
			break;
		case 2447:
			channel = 8;
			break;
		case 2452:
			channel = 9;
			break;
		case 2457:
			channel = 10;
			break;
		case 2462:
			channel = 11;
			break;
		case 2467:
			channel = 12;
			break;
		case 2472:
			channel = 13;
			break;
		case 2484:
			channel = 14;
			break;
		}
		return channel;
	}
}
